package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.dao.NewsDao;
import com.xyz.keshe.model.News;

public class NewsAdminDetailActivity extends AppCompatActivity {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvAuthor;
    private TextView tvCategory;
    private TextView tvDate;
    private TextView tvViewCount;
    private Button btnBack;
    private Button btnEdit;

    private NewsDao newsDao;
    private News news;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_admin_detail);

        // 初始化视图
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvAuthor = findViewById(R.id.tv_author);
        tvCategory = findViewById(R.id.tv_category);
        tvDate = findViewById(R.id.tv_date);
        tvViewCount = findViewById(R.id.tv_view_count);
        btnBack = findViewById(R.id.btn_back);
        btnEdit = findViewById(R.id.btn_edit);

        // 初始化DAO
        newsDao = new NewsDao(this);

        // 获取新闻ID
        Intent intent = getIntent();
        int newsId = intent.getIntExtra("news_id", 0);
        if (newsId == 0) {
            Toast.makeText(this, "新闻ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 获取新闻详情
        news = newsDao.getNewsById(newsId);
        if (news == null) {
            Toast.makeText(this, "新闻不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 设置新闻详情
        displayNewsDetails();

        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 设置编辑按钮点击事件
        btnEdit.setOnClickListener(v -> {
            Intent editIntent = new Intent();
            editIntent.putExtra("action", "edit");
            editIntent.putExtra("news_id", news.getId());
            setResult(RESULT_OK, editIntent);
            finish();
        });
    }

    private void displayNewsDetails() {
        tvTitle.setText(news.getTitle());
        tvContent.setText(news.getContent());
        
        String author = news.getAuthor();
        tvAuthor.setText("作者：" + (author != null && !author.isEmpty() ? author : "未知"));
        
        tvCategory.setText("栏目：" + news.getCategoryName());
        tvDate.setText("发布时间：" + news.getCreateTime());
        tvViewCount.setText("浏览量：" + news.getViewCount());
    }
} 